<template>
  <div id="login-container">
    <div class="login">
      <!-- 标题区域 -->
      <div class="title">
        <div class="logo">
          <img src="@/../public/favicon.png" alt="" />
        </div>
        <h1>Onepiece</h1>
      </div>
      <!-- 表单区域 -->
      <el-form
        ref="ruleFormRef"
        :rules="rules"
        :model="userInfo"
        label-width="70px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userInfo.username" :prefix-icon="User" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="userInfo.password"
            type="password"
            :prefix-icon="Notification"
          />
        </el-form-item>
        <!-- 登录 -->
        <div class="submit">
          <el-button
            type="primary"
            @click="loginInfo(ruleFormRef)"
            :loading="loading"
          >
            登录
          </el-button>
          <el-button type="success" @click="resetForm(ruleFormRef)">
            重置
          </el-button>
        </div>
      </el-form>
      <!-- 其他登录 -->
      <div class="otherLogin">
        <h3>其他登录</h3>
        <!-- SVG -->
        <div class="svg">
          <SvgIcon name="qq" width="50px" height="50px" />
          <SvgIcon name="wechat" width="50px" height="50px" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { FormInstance, FormRules, ElMessage } from 'element-plus'
import { User, Notification } from '@element-plus/icons-vue'
import 'element-plus/theme-chalk/el-message.css'
/* 引入用户数据仓库pinia */
import { userInfoStore } from '@/store/userLogin/user'
import router from '@/router'
import { getData } from '../../directives/utils/data'
const getUserState = userInfoStore()

/* 获取时间 */
const { str } = getData()
/* ref ===== dom元素 */
const ruleFormRef = ref<FormInstance>()
/* 输入信息 */
const userInfo = reactive({
  username: 'admin',
  password: 'atguigu123',
})
/* 表单校验规则 */
const rules = ref<FormRules>({
  username: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  password: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
    { min: 6, max: 12, message: 'Length should be 6 to 12', trigger: 'blur' },
  ],
})
/* 登录逻辑 */
/* 点击登录校验全部表单 */
const loginInfo = async (formRef: FormInstance | undefined) => {
  loading.value = true
  if (!formRef) return
  await formRef.validate(async (valid) => {
    if (valid) {
      /* 全部校验通过 */
      /* 执行登录逻辑 */
      try {
        await getUserState.userLogin(userInfo)
        loading.value = false
        ElMessage({
          type: 'success',
          message: `王先生${str.value}，恭喜登陆成功`,
        })
        router.push('/home')
      } catch (error) {
        ElMessage({
          type: 'error',
          message: (error as Error).message,
        })
      }
    } else {
      /* 校验失败 */
      ElMessage({
        type: 'error',
        message: '校验失败，重新输入',
      })
    }
  })
}
/* 重置表单 */
const resetForm = (formRef: FormInstance | undefined) => {
  if (!formRef) return
  /* 重置表单 */
  formRef.resetFields()
}
/* 按钮加载 */
const loading = ref(false)
</script>

<style scoped lang="scss">
@keyframes gradientBG {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}
#login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(90deg, #dfafda, #98daeb, #2575fc);
  background-size: 600% 600%;
  animation: gradientBG 5s ease infinite;
  /* 登陆模态框 */
  .login {
    width: 18.75rem;
    height: 31.25rem;
    padding: 0 1.075rem;
    border-radius: 2.1875rem;
    box-shadow: 0 0 #fff, 0 0 10px #fff, 0 0 #fff, 0 0 #ffffff, 0 0 10px #6d6d6d,
      0 0 10px #aeacac, 0 0 10px #cdcaca, 0 0 10px #c1c1c1;
    // background-color: #ffffff;
    /* 名称 */
    .title {
      margin: 1.875rem 0;
      display: flex;
      justify-content: center;
      align-items: center;
      /* logo */
      .logo {
        width: 6rem;
        height: 6rem;
        img {
          width: 100%;
          height: 100%;
          vertical-align: middle;
        }
      }
      /* 标题 */
      h1 {
        font-size: 2.0875rem;
        font-weight: 900;
        color: #00cf74;
      }
    }
    /* 表单 */
    .el-form {
      margin-top: 3.25rem;
      .submit {
        text-align: center;
        margin-top: 2.125rem;
        .el-button {
          margin-left: 1.375rem;
        }
      }
    }
    /* 其他登录 */
    .otherLogin {
      margin-top: 30px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .svg {
        margin-top: 30px;
        display: flex;
        justify-content: center;
        align-items: center;
        svg {
          margin: 0 0.625rem;
        }
      }
    }
  }
}
</style>
